/**
 * @file
 * @description 公共库
 */

import { MessageBox } from "element-ui"

/**
 * @method
 * @description 询问方法
 * @param {String} content  提示的内容
 * @param {String} title  提示的标题
 */
export const confirm = (message, title = '提示') => {
    return new Promise((resolve, reject) => {
        MessageBox.confirm(message, title, {
            callback(action) {
                switch (action) {
                    case 'confirm':
                        resolve()
                        break;
                    case 'cancel':
                        reject()
                        break;
                }
            }
        })
    })
}

/**
 * @method
 * @description 深拷贝
 */
export const deepClone = (source) => {
    const newObj = Array.isArray(source) ? [] : {};//新对象

    /**
     * @method
     * @description 拷贝方法
     * @param {Object} dest  目标对象
     * @param {Object} sour   深对象
     */
    const clone = (dest, sour) => {
        for (const key in sour) {
            if (Object.hasOwnProperty.call(sour, key)) {
                const item = sour[key]
                if (item instanceof RegExp) {
                    dest[key] = new RegExp(item);
                } else if (item instanceof Date) {
                    dest[key] = new Date(item);
                } else if (item instanceof Object) {
                    clone((dest[key] = Array.isArray(item) ? [] : {}), item)
                } else {
                    dest[key] = item;
                }
            }
        }
    }
    clone(newObj, source);
    return newObj;
}